#include "include/mainItem.h"
#include <QString>

MainItem::MainItem(QWidget *parent)
    : Item(parent)
{
    CreateWidget();
    CreateLayout();
}

void MainItem::CreateWidget()
{   
    id = new QLabel(QString::fromUtf8("*"), this);
    pos = new QLabel(QString::fromUtf8("*区*栋*层*房"), this);
//    QLabel pos="*";
//    pos +=QString::fromUtf8("区");
    icon = new QLabel(QString::fromUtf8(" "), this);
    launch = new QLabel(QString::fromUtf8("启动"), this);
    feedback = new QLabel(QString::fromUtf8("反馈"), this);
    malfunction = new QLabel(QString::fromUtf8("故障"), this);

    QFont posFont = pos->font();
    posFont.setPointSize(11);
    posFont.setBold(true);
    pos->setFont(posFont);

    icon->setAlignment(Qt::AlignCenter);
    launch->setAlignment(Qt::AlignCenter);
    feedback->setAlignment(Qt::AlignCenter);
    malfunction->setAlignment(Qt::AlignCenter);

    this->setStyleSheet("QPushButton{background-color : white}");
    launch->setStyleSheet("QLabel{background-color:green;}");
    feedback->setStyleSheet("QLabel{background-color:green;}");
    malfunction->setStyleSheet("QLabel{background-color:green;}");
}

void MainItem::CreateLayout()
{
//    QHBoxLayout *rightBottom = new QHBoxLayout;
//    rightBottom->addWidget(launch);
//    rightBottom->addWidget(feedback);
//    rightBottom->addWidget(malfunction);

//    QVBoxLayout *rightLayout = new QVBoxLayout;
//    rightLayout->addWidget(pos);
//    rightLayout->addLayout(rightBottom);

//    QVBoxLayout *leftLayout = new QVBoxLayout;
//    leftLayout->addWidget(id);
//    leftLayout->addWidget(icon);

//    QHBoxLayout *mainLayout = new QHBoxLayout;
//    mainLayout->addLayout(leftLayout);
//    mainLayout->addLayout(rightLayout);
    QHBoxLayout *topBottom = new QHBoxLayout;
    topBottom->addWidget(id);
    topBottom->addWidget(pos);

    QHBoxLayout *rightBottom = new QHBoxLayout;
    rightBottom->addWidget(icon);
    rightBottom->addWidget(launch);
    rightBottom->addWidget(feedback);
    rightBottom->addWidget(malfunction);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topBottom);
    mainLayout->addStretch();
    mainLayout->addLayout(rightBottom);

    setLayout(mainLayout);

    QSizePolicy policy = sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    setSizePolicy(policy);

    policy = id->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    id->setSizePolicy(policy);

    policy = icon->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    icon->setSizePolicy(policy);

    policy = pos->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    pos->setSizePolicy(policy);

    policy = launch->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    launch->setSizePolicy(policy);

    policy = feedback->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    feedback->setSizePolicy(policy);

    policy = malfunction->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    malfunction->setSizePolicy(policy);


}

void MainItem::SwitchToObject()
{

}

void MainItem::run()
{
    while(1)
    {
        launch->setStyleSheet("QLabel{background-color:red;}");
        for (int i=100000; i>0; i--);
        launch->setStyleSheet("QLabel{background-color:blue;}");
        for (int i=100000; i>0; i--);
    }
}

void MainItem::LaunchBlink()
{
    while(1)
    {
        launch->setStyleSheet("QLabel{background-color:red;}");
        for (int i=100000; i>0; i--);
        launch->setStyleSheet("QLabel{background-color:blue;}");
        for (int i=100000; i>0; i--);
    }
}

void MainItem::FeedbackBlink()
{

}

void MainItem::MalfunctionBlink()
{

}
